/**
 * DOM 操作封装
 */

//调单个节点
function getEle(selector) {
  return document.querySelector(selector)
}

//调多个节点
function getEleAll(selector) {
  return document.querySelectorAll(selector)
}

//事件加载事件
function reday(callback) {
  window.addEventListener('DOMContentLoaded', function () {
    callback()
  })
}

//封装点击事件
function getEvent(node, callback, type = 'click') {
  document.querySelector(node).addEventListener(type, function () {
    callback()
  })
}

//获取本地存储token值
let tokenV = localStorage.getItem('token')

//判定是否放行
function getToken() {
  if (!tokenV) {
    location.href = 'login.html'
  }
}

//尾部公共样式
function footerH(pos) {
  getEle('body').innerHTML += `
    <footer>
    <ul>
      <li>
        <a class="${pos == 'home' ? 'active' : ''}" href="home.html"><span class="iconfont icon-home"></span>
          首页</a>

      </li>
      <li>
        <a class="${pos == 'sports' ? 'active' : ''}" href="run.html"><span class="iconfont icon-frequency"></span>
          运动</a>
        
      </li>
      <li>
        <a class="${pos == 'circle' ? 'active' : ''}" href="circle.html"><span class="iconfont icon-circle"></span>
          圈子</a>
        
      </li>
      <li>
        <a class="${pos == 'my' ? 'active' : ''}" href="my.html"><span class="iconfont icon-mine"></span>
          我的</a>
        
      </li>
    </ul>
  </footer>
    `
}


// 顶部公共样式
function headerH(pos) {
  getEle('header').innerHTML = `
  <header>
  <div class="df word">
      <div>
          <a class="${pos=='run'?'active':''}" href="run.html">跑步</a>
          <span class="${pos=='run'?'dblock':''}"></span>
      </div>
      <div>
          <a class="${pos=='cycling'?'active':''}" href="cycling.html">骑行</a>
          <span class="${pos=='cycling'?'dblock':''}"></span>
      </div>
      <div>
          <a class="${pos=='courseTraining'?'active':''}" href="courseTraining.html">课程训练</a>
          <span class="${pos=='courseTraining'?'dblock':''}"></span>
      </div>

  </div>

</header>
  `
}

//暴露文件
module.exports = {
  getEle,
  getEleAll,
  reday,
  getEvent,
  getToken,
  footerH,
  headerH
}
